<?php
/* 
 *供应商管理
 */
class SupplierAction extends CommonAction {
    public function _filter(&$map) {
        $key = trim($_REQUEST['keywords']);
        if($_REQUEST['custom'] == 1 ) //定制搜索
        {
            $map['Reaction_type.name'] = array('like',"%".trim($_REQUEST['reaction'])."%");
            $map['Product_class.name'] = array('like',"%".trim($_REQUEST['pclass'])."%");
            //$map['_logic'] = "and";
        }
        else if(!empty($key)) {
            $map['name'] = array('like',"%".$key."%");
            $map['ename'] = array('like',"%".$key."%");
            $map['phone'] = array('like',"%".$key."%");
            $map['email'] = array('like',"%".$key."%");
            $map['_logic'] = "or";
        }
        
    }

    //适用于定制搜索
    public function view_search()
    {
        $this->index('SupplierCustomView');
    }


    public function _before_edit()
    {
        $assign_data = array();//assign data to page
        $map = 'supplier_id = '.intval($_REQUEST['id']) ;
        //分页
        import ( "@.ORG.Page" );
        $map_his_offfers = 'supplier_id = '.$_REQUEST['id'] ;
        $count_his_offers = M('Ask_supplier_detail')->where($map_his_offfers) ->count(); 
        if($count_his_offers > 0){
            $page0 = new Page($count_his_offers,25);
            $his_offers = M('Ask_supplier_detail')->where($map_his_offfers)->limit($page0->firstRow.','.$page0->listRows)->order('update_time desc')->findAll(); 
            $assign_data['his_offers'] = $his_offers;
            $assign_data['page_his_offers'] = $page0->show();
        }
        $count_contacts = M('Contact')->where($map) ->count(); 
        if($count_contacts > 0){
            $page_contacts = new Page($count_contacts,25);
            $contacts = getModelField('Contact','*',$map,'',$page_contacts->firstRow.','.$page_contacts->listRows);
            
            $assign_data['contacts'] = $contacts;
            $assign_data['page_contacts'] = $page_contacts->show();
        }
/*
        $count_ask_suppliers = M('Ask_supplier')->where($map) ->count(); 
        if($count_ask_suppliers > 0){
            $page_ask_suppliers = new Page($count_ask_suppliers,25);
            $ask_suppliers = getModelField('Ask_supplier','*',$map,'',$page_ask_suppliers->firstRow.','.$page_ask_suppliers->listRows);
            
            $assign_data['ask_suppliers'] = $ask_suppliers;
            $assign_data['page_ask_suppliers'] = $page_ask_suppliers->show();
        }
*/
        $count_buys = M('Buy')->where($map) ->count(); 
        if($count_buys > 0){
            $page_buys = new Page($count_buys,25);
            $buys = getModelField('Buy','*',$map,'',$page_buys->firstRow.','.$page_buys->listRows);
            
            $assign_data['buys'] = $buys;
            $assign_data['page_buys'] = $page_buys->show();
        }
        $this->assign($assign_data);
       
    }

     public function insert(){
        
        $_POST['name'] = trim($_POST['name']);
        
        //B('FilterString');
        $name=$this->getActionName();
        $model = D ($name);
        if (false === $model->create ()) {
            $this->error ( $model->getError () );
        }
        //保存当前数据对象
        $list=$model->add ();
        if ($list!==false) { //保存成功
			if(!empty($_POST['contact_id'])){
				$data['supplier_id'] = $list;
				$data['name'] = $_POST['contact_id'];
                $data['qq'] = $_POST['qq'];
                $data['mobile'] = $_POST['mobile'];
                $data['phone'] = $_POST['phone'];
				$data['status'] = 1;
				$contact_id = M('Contact')->data($data)->add();
				if($contact_id)
					M('Supplier')->where('id ='. $list)->setField('contact_id',$contact_id); //更新联系人
			}

             $mod_field = getModelName ($list, $name,$field);
            //如果未取到字段值 ，则返回对应的ID编号
            $msg = '新增 '.$name.': '.$mod_field.' (编号：'.$list.')';
            if('Action' != $name)
                A('Action')->writeLog($msg,'新增',$name,$list);

            if(!empty($_REQUEST['jumpUrl'])){   //返回原操作新增页面
                $jumpUrl = '__APP__/'.$_REQUEST['jumpUrl'].'/add/supplier_id/'.$list;
            }
            if(!empty($_REQUEST['rid'])){ //如果有rid，返回编辑页
                $jumpUrl = '__APP__/'.$_REQUEST['jumpUrl'].'/edit/id/'.$_REQUEST['rid'].'/supplier_id/'.$list;
                if($_REQUEST['jumpUrl'] == 'Ask_supplier_detail' ) //供应商报价
                $jumpUrl = '__APP__/'.$_REQUEST['jumpUrl'].'/add/asid/'.$_REQUEST['rid'].'/supplier_id/'.$list;
                
            }


            if(empty($jumpUrl))
                $jumpUrl = Cookie::get ( '_currentUrl_' );
            $this->assign ( 'jumpUrl',  $jumpUrl);
            $this->success ('新增成功!');
        } else {
            //失败提示
            $this->error ('新增失败!');
        }
    }

    public function edit_rank(){
        $this->edit('edit_rank');    
    }
    
    //相关产品
    public function plist($value='')
    {

        $map = 'supplier_id = '.intval($_REQUEST['id']) ;
        $count = D('SProductView')->where($map)->count();
        if($count > 0){
            import ( "@.ORG.Page" );
            $page = new Page($count,25);
            $ps = D('SProductView')->where($map)->limit($page->firstRow.','.$page->listRows)->order('spid desc')->findAll(); 
            $assign_data = array(
                'products' => $ps,
                'page' => $page->show(),
            ); 
            $this->assign($assign_data);
        }
        $this->display();
    }

   //相关产品类别--定制
    public function product_class()
    {

        $map = 'supplier_id = '.intval($_REQUEST['id']) ;
        $count = M('Supplier_product_class')->where($map)->count();
        if($count > 0){
            import ( "@.ORG.Page" );
            $page = new Page($count,25);
            $ps = M('Supplier_product_class')->where($map)->limit($page->firstRow.','.$page->listRows)->findAll(); 
            $assign_data = array(
                'products' => $ps,
                'page' => $page->show(),
            ); 
            $this->assign($assign_data);
        }
        $this->display();
    }

    //相反应类型--特色
    public function reaction_type()
    {

        $map = 'supplier_id = '.intval($_REQUEST['id']) ;
        $count = M('Supplier_reaction_type')->where($map)->count();
        if($count > 0){
            import ( "@.ORG.Page" );
            $page = new Page($count,25);
            $ps = M('Supplier_reaction_type')->where($map)->limit($page->firstRow.','.$page->listRows)->findAll(); 
            $assign_data = array(
                'products' => $ps,
                'page' => $page->show(),
            ); 
            $this->assign($assign_data);
        }
        $this->display();
    }

}
?>
